// packageA/role/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        roles: [],
        filteredRoles: [{
            name: '超级管理员',
            code: 'superadmin',
            createTime:'2025-09-20'
        },],
        addModalVisible: false,
        editModalVisible: false,
        deleteModalVisible: false,
        newRole: {
            name: '',
            code: ''
        },
        currentRole: {},
        searchText: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.loadRoles();
    },
    loadRoles() {
        const roles = wx.getStorageSync('roles') || [];
        if(roles.length>0)
        this.setData({
            roles,
            filteredRoles: roles
        });
    },

    saveRoles(roles) {
        wx.setStorageSync('roles', roles);
        this.loadRoles();
    },
    onSearchInput(e) {
        const searchText = e.detail.value.toLowerCase();
        const filteredRoles = this.data.roles.filter(role =>
            role.name.toLowerCase().includes(searchText) ||
            role.code.toLowerCase().includes(searchText)
        );
        this.setData({
            searchText,
            filteredRoles
        });
    },

    showAddModal() {
        this.setData({
            addModalVisible: true,
            newRole: { name: '', code: '' }
        });
    },

    hideAddModal() {
        this.setData({ addModalVisible: false });
    },

    showEditModal(e) {
        this.setData({
            editModalVisible: true,
            currentRole: { ...e.currentTarget.dataset.item }
        });
    },

    hideEditModal() {
        this.setData({ editModalVisible: false });
    },

    showDeleteModal(e) {
        this.setData({
            deleteModalVisible: true,
            deleteRoleCode: e.currentTarget.dataset.code
        });
    },

    hideDeleteModal() {
        this.setData({ deleteModalVisible: false });
    },

    onNameInput(e) {
        this.setData({
            'newRole.name': e.detail.value
        });
    },

    onCodeInput(e) {
        this.setData({
            'newRole.code': e.detail.value
        });
    },

    onEditNameInput(e) {
        this.setData({
            'currentRole.name': e.detail.value
        });
    },

    onEditCodeInput(e) {
        this.setData({
            'currentRole.code': e.detail.value
        });
    },

    addRole() {
        const { name, code } = this.data.newRole;
        if (!name || !code) {
            wx.showToast({ title: '请填写完整信息', icon: 'none' });
            return;
        }

        const roles = [...this.data.roles];
        if (roles.some(role => role.code === code)) {
            wx.showToast({ title: '角色代码已存在', icon: 'none' });
            return;
        }

        const newRole = {
            name,
            code,
            createTime: this.formatTime(new Date())
        };

        roles.push(newRole);
        this.saveRoles(roles);
        this.hideAddModal();
        wx.showToast({ title: '添加成功' });
    },

    updateRole() {
        const { name, code } = this.data.currentRole;
        if (!name || !code) {
            wx.showToast({ title: '请填写完整信息', icon: 'none' });
            return;
        }

        const roles = this.data.roles.map(role => {
            if (role.code === this.data.currentRole.code) {
                return { ...role, name, code };
            }
            return role;
        });

        this.saveRoles(roles);
        this.hideEditModal();
        wx.showToast({ title: '更新成功' });
    },

    deleteRole() {
        const roles = this.data.roles.filter(role => role.code !== this.data.deleteRoleCode);
        this.saveRoles(roles);
        this.hideDeleteModal();
        wx.showToast({ title: '删除成功' });
    },

    formatTime(date) {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})